<?php
// +----------------------------------------------------------------------
// | @filename MenuLogic.php
// +----------------------------------------------------------------------
// | @author 落花轻舞问君愁 <mwcx1990@qq.com>
// +----------------------------------------------------------------------
// | @datetime 2021-11-01 17:57:20
// +----------------------------------------------------------------------
// | @Description
// +----------------------------------------------------------------------

namespace Addons\Backend\Logics;


use Addons\Backend\Helpers\ArrayHelper;
use Addons\Backend\Repositories\MenuHasRoleRepository;
use Addons\Backend\Repositories\MenuRepository;
use Addons\Backend\Transformers\MenuTransformer;

class MenuLogic extends BaseLogic
{
    protected $menuRepository;
    protected $menuHasRoleRepository;
    protected $menuTransformer;

    public function __construct(MenuRepository $menuRepository, MenuHasRoleRepository $menuHasRoleRepository, MenuTransformer $menuTransformer)
    {
        $this->menuRepository = $menuRepository;
        $this->menuHasRoleRepository = $menuHasRoleRepository;
        $this->menuTransformer = $menuTransformer;
    }

    /**
     * 获取菜单树
     * @param
     * @return array
     */
    public function orderBySortTree(): array
    {
        $menus = $this->menuRepository->orderBySortAll();
        return $this->success(['list' => ArrayHelper::makeTree(fractal($menus, $this->menuTransformer)->toArray()['list'])]);
    }

    /**
     * 存储菜单
     * @param $data
     * @return array
     */
    public function store($data): array
    {
        $roles = $data['roles'];
        unset($data['roles']);
        $data['pid'] = !empty($data['pid']) ? $data['pid'] : 0;

        if (empty($data['id'])) {
            $menu = $this->menuRepository->create($data);
        } else {
            $menu = $this->menuRepository->update($data);
        }

        //删除现有的中间表关系记录
        $this->menuHasRoleRepository->deleteByCondition([['menu_id', '=', $menu->id]]);

        // 添加新的中间表关系记录
        if (!empty($roles)) {
            $this->createMenuHasRoleData($menu->id, $roles);
        }

        return $this->success(fractal($menu, $this->menuTransformer)->toArray(), empty($data['id']) ? '创建菜单操作成功' : '更新菜单操作成功');
    }

    public function createMenuHasRoleData($menuId, $roleIds)
    {
        $menuHasRoleData = [];

        foreach ($roleIds as $roleId) {
            $menuHasRoleData[] = [
                'menu_id' => $menuId,
                'role_id' => $roleId
            ];
        }

        if (!empty($menuHasRoleData)) {
            return $this->menuHasRoleRepository->batchInsert($menuHasRoleData);
        }
    }

    /**
     * 删除权限
     * @param $id
     * @return array
     */
    public function delete($id): array
    {
        $menu = $this->menuRepository->findById($id);

        if (empty($menu)) {
            return $this->failed('菜单不存在');
        }

        $childrenMenus = $this->menuRepository->getChildrenByPid($id);

        if (!empty($childrenMenus)) {
            return $this->failed('当前菜单存在子菜单,请先删除子菜单后再试');
        }

        return $this->menuRepository->delete($id) ? $this->success([], '删除菜单操作成功') : $this->failed([], '删除菜单操作失败');
    }
}
